package au.com.iglootech.winerymap.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import au.com.iglootech.winerymap.entity.WineryInfo;
import au.com.iglootech.winerymap.service.WineryInfoService;
import au.com.iglootech.winerymap.service.dao.GenericDao;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

/**
 * 
 * @author Nicholas Zhu
 * 
 */
@Component(WineryInfoServiceImpl.BEAN_NAME)
public class WineryInfoServiceImpl implements WineryInfoService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2697727619376771819L;

	public static final String BEAN_NAME = "WineryInfoServiceImpl";

	@Autowired
	private transient GenericDao genericDao;

	@Override
	public List<WineryInfo> findAll() {
		return new ArrayList<WineryInfo>(genericDao.findAll(WineryInfo.class));
	}

	@Override
	public WineryInfo findByKey(final Key key) {
		return genericDao.findByKey(WineryInfo.class, key);
	}

	@Override
	public WineryInfo createWineryInfo(final WineryInfo info) {
		return genericDao.save(info);
	}

	@Override
	public String addNewWineryInfo(String name, String region, String address1,
			String address2, String website, String telephone,
			String winemaker, String wineryards, String facsimilie,
			String description, String lat, String lng) {
		WineryInfo info = new WineryInfo();
		info.setTradeName(name);
		info.setCompanyName(name);
		info.setAddress1(address1);
		info.setAddress2(address2);
		info.setRegion(region);
		info.setWebsite(website);
		info.setTelephone(telephone);
		info.setWinemaker(winemaker);
		info.setWineryards(wineryards);
		info.setFacsimilie(facsimilie);
		info.setDescription(new Text(description));
		if(!"".equals(lat) && !"".equals(lng))
		{
			info.setLat(Double.valueOf(lat));
			info.setLng(Double.valueOf(lng));
		}

		genericDao.save(info);
		return info.getKeyValue();
	}

}
